<!--企业组织新增modal-->
<template>

  <a-modal
    :title="title + '企业组织'"
    width='60%'
    :visible='visible'
    :confirmLoading='confirmLoading'
    switchFullscreen
    @ok='handleOk'
    @cancel='handleCancel'
    cancelText='关闭'
    style='height: 100%;overflow: auto;padding-bottom: 53px;'>
    <template slot='footer'>
      <a-button @click='handleCancel'>取消</a-button>
      <a-button @click='handleOk' type='primary' :loading='confirmLoading'>确认</a-button>
    </template>
    <a-spin :spinning='confirmLoading'>
      <a-form :form='form'>
        <a-row :gutter='24'>
          <a-col :md='12' :sm='24'>
            <a-form-item label='编码' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input placeholder='请输入仓库编码' v-decorator="['warehouseCode', validatorRules.warehouseCode]" />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='组织类型'>
              <cn-dict-select-tag
                @change='orgTypeC'
                :options="{ value: 'dictDataCode', label: 'dictDataDesc' }"
                :triggerChange='true'
                dictCode='org_type'
                v-decorator="['orgType', validatorRules.orgType]"
                placeholder='请选择组织类型'
              />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='状态'
              >
              <cn-dict-select-tag :options="{value:'dictDataCode',label:'dictDataDesc'}" :triggerChange='true'
                                 dictCode='status'
                                 v-decorator="['status',validatorRules.status]" placeholder='请选择状态' />
            </a-form-item>
          </a-col>
          <a-col :md='12' :sm='24' v-if='topOrg'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='上级组织'
              >
              <cn-dict-select-tag :options="{value:'dictDataCode',label:'dictDataDesc'}" :triggerChange='true'
                                 dictCode='status'
                                 v-decorator="['status',validatorRules.status]" placeholder='请选择状态' />
            </a-form-item>
          </a-col>
          <a-col :md='20' :sm='24'>
            <a-form-item
              :labelCol='labelCol1'
              :wrapperCol='wrapperCol1'
              label='备注'>
              <a-textarea style='min-height: 60px' placeholder='请输入备注' v-decorator="['remarks', {}]" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>

    </a-spin>
  </a-modal>

</template>

<script>
import CodeConfigModal from '@/components/cn-select-popup/CodeConfigModal'
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import AFormItem from 'ant-design-vue/es/form/FormItem'
import ATextarea from 'ant-design-vue/es/input/TextArea'
import JImageUpload from '@/components/cn-form/JImageUpload'
import JTreeSelect from '@/components/cn-form/JTreeSelect'
import JSelectUserByDep from '@/components/cn-select-popup/JSelectUserByDep'
import JSelectTerminal from '@/components/cn-select-popup/JSelectTerminal'

export default {
  name: 'companOrgModal',
  components: {
    JImageUpload,
    AFormItem,
    ATextarea,
    JTreeSelect,
    JSelectUserByDep,
    JSelectTerminal,
    CodeConfigModal
  },
  data() {
    return {
      topOrg:false,
      custCode: '',
      custName: '',
      showCode: 4,
      terminalName: '',
      terminalCode: '',
      title: '操作',
      visible: false,
      selecttype: '',
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 }
      },
      labelCol1: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol1: {
        xs: { span: 24 },
        sm: { span: 21 }
      },
      confirmLoading: false,

      form: this.$form.createForm(this),

      validatorRules: {
        warehouseCode: {
          rules: [{
            required: true, message: '请输入仓库编码!', trigger: 'submit'
          }]
        },
        warehouseName: {
          rules: [{
            required: true, message: '请输入仓库名称!', trigger: 'submit'
          }]
        },
        warehouseType: {
          rules: [{
            required: true, message: '请选择类型!', trigger: 'submit'
          }]
        },
        status: {
          rules: [{
            required: true, message: '请选择状态!', trigger: 'submit'
          }]
        }
      },
      url: {
        add: window._CONFIG['domianURL'] + '/warehouse/cnWarehouse/saveOrUpdate'
        // edit: window._CONFIG['domianURL'] + '/terminal/cnTerminalPool/updateAmount'
      }
    }
  },
  methods: {
    orgTypeC(e1, e2) {
      if (e1 == 1) {
        this.topOrg = false
      } else {
        this.topOrg = true
      }
    },
    handelSaveOk(value) {
      this.custName = value.name
      this.custCode = value.code
    },
    handleToCust() {
      this.$refs.CodeConfigModal.showModal()
    },
    selects(value) {
      this.terminalName = value[0].terminalName
      this.terminalCode = value[0].terminalCode
    },
    dotype(val) {
      this.selecttype = val
    },
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'warehouseCode', 'warehouseName',
          'warehouseType', 'custCode', 'custName', 'status'))
      })
    },
    close() {
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          httpurl += this.url.add
          let method = ''
          method = 'post'
          let formData = Object.assign(this.model, values)
          if (this.title === '新增') {
            formData.custCode = this.custCode
            formData.custName = this.custName
          }
          //时间格式化
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>

<style scoped>
.drawer-bootom-button {
  position: absolute;
  bottom: -8px;
  width: 100%;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  left: 0;
  background: #fff;
  border-radius: 0 0 2px 2px;
}
</style>